package com.cx.core.magazine.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cx.core.magazine.dao.PeriodDao;
import com.cx.core.magazine.model.bean.Period;
import com.li.core.daoBase.DAOBase;

public class PeriodDaoImpl extends DAOBase implements PeriodDao {

	@Override
	public List<Period> queryAllPeriod(int page) {

		int start, end;
		start = (page - 1) * 50;
		end = page * 30;
		List<Period> periods = new ArrayList<Period>();
		String sql = "select * from period order by period_id limit " + start
				+ "," + end;
		List list = this.getJdbcT().queryForList(sql);
		Iterator iterator = list.iterator();
		Period period = null;
		while (iterator.hasNext()) {
			Map mapPeriod = (Map) iterator.next();
			period = new Period();
			period.setPeriod_id((Integer) mapPeriod.get("period_id"));
			period.setMag_id((Integer) mapPeriod.get("mag_id"));
			period.setPeriod((Integer) mapPeriod.get("period"));
			period.setPeriod_id((Integer) mapPeriod.get("period_id"));
			period.setYear_period((Integer) mapPeriod.get("year_period"));
			period.setPublisher((String) mapPeriod.get("publisher"));
			period.setPublish_time(((Date) mapPeriod.get("publish_time"))
					.getTime());
			periods.add(period);
		}
		return periods;
	}

	@Override
	public Period queryPeriodById(int periodId) {
		Integer[] params = new Integer[] { periodId };
		String sql = "select * from period where period_id = ? order by period_id";
		List list = this.getJdbcT().queryForList(sql, params);
		Iterator iterator = list.iterator();
		Period period = null;
		while (iterator.hasNext()) {
			Map mapPeriod = (Map) iterator.next();
			period = new Period();
			period.setPeriod_id((Integer) mapPeriod.get("period_id"));
			period.setMag_id((Integer) mapPeriod.get("mag_id"));
			period.setPeriod((Integer) mapPeriod.get("period"));
			period.setPeriod_id((Integer) mapPeriod.get("period_id"));
			period.setYear_period((Integer) mapPeriod.get("year_period"));
			period.setCover_id(String.valueOf(mapPeriod.get("cover_Id")));
			period.setPublisher((String) mapPeriod.get("publisher"));
			period.setPublish_time(((Date) mapPeriod.get("publish_time"))
					.getTime());
			period.setYear((Integer)mapPeriod.get("year"));
			
		}
		return period;
	}

	@Override
	public int SavePeriod(Period period) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Period> queryPeriodByYear(int year) {
		Integer[] params = new Integer[] { year };
		List<Period> periods = new ArrayList<Period>();
		String sql = "select * from period where year = ? order by period_id";
		List list = this.getJdbcT().queryForList(sql, params);
		Iterator iterator = list.iterator();
		Period period = null;
		while (iterator.hasNext()) {
			Map mapPeriod = (Map) iterator.next();
			period = new Period();
			period.setPeriod_id((Integer) mapPeriod.get("period_id"));
			period.setMag_id((Integer) mapPeriod.get("mag_id"));
			period.setPeriod((Integer) mapPeriod.get("period"));
			period.setPeriod_id((Integer) mapPeriod.get("period_id"));
			period.setYear_period((Integer) mapPeriod.get("year_period"));
			period.setCover_id(String.valueOf(mapPeriod.get("cover_Id")));
			period.setPublisher((String) mapPeriod.get("publisher"));
			period.setPublish_time(((Date) mapPeriod.get("publish_time"))
					.getTime());
			periods.add(period);
		}
		return periods;
	}

	@Override
	public ArrayList<String> queryYears() {
		ArrayList<String> years = new ArrayList<String>();
		String sql = "select DISTINCT(year) `year` from period order by year";
		List list = this.getJdbcT().queryForList(sql);
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Map mapYear = (Map) iterator.next();
			years.add(mapYear.get("year").toString());
		}
		return years;
	}

	@Override
	public ArrayList<String> queryPeriods(int year) {
		Integer[] params = new Integer[] { year };
		ArrayList<String> periods = new ArrayList<String>();
		String sql = "select year_period,period_id from period where year = ? order by year_period";
		List list = this.getJdbcT().queryForList(sql,params);
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			Map mapPeriod = (Map) iterator.next();
			periods.add(mapPeriod.get("year_period").toString()+"-"+mapPeriod.get("period_id").toString());
		}
		return periods;
	}

}
